cap log close
clear all

if c(username) == "matteobobba" {
	global JPE `"/Users/matteobobba/Dropbox/Research/API/Submissions/1. JPE/5. June 2024"'
	}

	
**********************************************
 /* Table 1: Differences Across Populations */
**********************************************

use "${JPE}/Data/scaleup_localities.dta", clear

global enlace10 cal_esp10 cal_mat10 
global loc poverty_high confl_d share_illi share_econ noroad paved_road pobtot water sewer waste
global school repeat2012 overage2012 enrollment2012 teachers2012

balancetable (mean if sample==1) (mean if sample==3) (mean if sample==2) (diff exp1 if sample!=2) (diff exp2 if sample!=3) $enlace10 $school $loc using "${JPE}/Draft/Tables/table1.tex", varlabels pval vce(robust) ctitles("All Chiapas" "First Experiment" "Second Experiment" "All vs Experiment 1" "All vs Experiment 2" ) replace

********************************************************
 /* Table 2: Children's Achievement -- 1st Experiment */
********************************************************

use "${JPE}/Data/exp1_students.dta", clear

global regressors "i.enti"
global regressors1 "i.enti male"
global regressors2 "i.enti male enl_spanish_2010 enl_math_2010"

replace margina=999 if margina==.
gen missmargina=(margina==999)

swindex enl_esp_13 enl_mat_13 enl_third_13, gen(index_13)

foreach var of varlist enl_esp_13 enl_mat_13 enl_third_13 index_13 {
qui sum `var' if treat==0
gen st`var'=(`var'-r(mean))/r(sd)
}

local reps=3000
local seed=123
qui rwolf2 (reg stenl_esp_13 treat $regressors1, vce(cl ID02)) (reg stenl_mat_13 treat $regressors1, vce(cl ID02)) (reg stenl_third_13 treat $regressors1, vce(cl ID02)) (reg stindex_13 treat $regressors1, vce(cl ID02)), cluster(ID02) indepvars(treat, treat, treat, treat) usevalid seed(`seed') reps(`reps') nodots strata(enti)
mat RW_treat=e(RW)

local k=1

foreach var of varlist stenl_esp_13 stenl_mat_13 stenl_third_13 stindex_13 {
	qui reg `var' treat $regressors1, cluster(ID02)
	qui mat p_rw = RW_treat[`k',3]
	qui mat colnames p_rw = treat
	qui estadd matrix p_rw 
	qui eststo `var'

	qui ritest treat _b[treat]/_se[treat] , seed(`seed') reps(`reps') nodots cluster(ID02)  strata(enti): reg `var' treat $regressors1, cluster(ID02)
	qui matrix p_ri=r(p)
	qui mat colnames p_ri = treat
	qui est restore `var'
	qui estadd matrix p_ri
	qui eststo `var' 
	local k=`k'+1
}

estout stenl_esp_13 stenl_mat_13 stenl_third_13 stindex_13 using "${JPE}/Draft/Tables/table2.tex", replace style(tex) cells(b(star fmt(%9.3f)) p(par([ ]) fmt(%9.3f)) p_ri(par(\{  \})) p_rw(par(( ))) .)  stats(N_clust N, fmt(%9.0f %9.0f) label("Number of Schools" "Number of Observations")) label collabels(none) mlabels(none) varlabels(treat "API Original")   keep(treat)  


***********************************************************************
 /* Table 3: Children's Achievement and Attainment -- 2nd Experiment */
***********************************************************************
use "${JPE}/Data/exp2.dta", clear

* Survey outcomes (COLS 1-4)
replace age=age/12

foreach var in enroll_sec age_tr male_tr trans_year{
	gen st_`var'=st_`var'_1 if st_trans_year_1==1
	replace st_`var'=st_`var'_2 if st_trans_year_2==2
	}

gen enr_old=st_enroll_sec_2 if st_age_tr_2>=13
swindex st_egra_2 st_egma_2 st_tot_dum_e_2 if merge_ind_par==3, gen(st_index_2)

foreach x of varlist st_egra_2 st_egma_2 st_tot_dum_e_2 st_index_2 {
	gen `x'_sd = .
	qui sum `x' if treat==0  & merge_ind_par==3
	replace `x'_sd =(`x'-r(mean))/r(sd) 
	}

forvalues j = 0(1)2{
	gen treat`j' = 0
	replace treat`j' = 1 if treat==`j'
	replace treat`j' = . if treat==.
}

local reps=3000
local seed=123
qui rwolf2 (reg st_egra_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_egma_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_tot_dum_e_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2, vce(cl cct)) (reg st_index_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat1, treat1, treat1, treat1) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_stand=e(RW)

qui rwolf2 (reg st_egra_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_egma_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_tot_dum_e_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2, vce(cl cct)) (reg st_index_2_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=e(RW)

qui rwolf2 (reg st_egra_2_sd treat0 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_egma_2_sd treat0 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)) (reg st_tot_dum_e_2_sd treat0 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2, vce(cl cct)) (reg st_index_2_sd treat0 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_diff=e(RW)


local k=1

foreach x of varlist st_egra_2_sd st_egma_2_sd st_tot_dum_e_2_sd st_index_2_sd{
	qui reg `x' treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)			
	qui estadd scalar obs = e(N)
	qui estadd scalar clusters = e(N_clust)
	qui estadd scalar adjR2 = e(r2_a)
	qui test treat1=treat2
	qui estadd scalar p_diff=r(p)
	qui eststo `x' 

	qui ritest treat _b[1.treat]/_se[1.treat] , fixlevels(2) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2  i.st_num_ruta_2   if merge_ind_par==3, vce(cl cct)
	qui matrix pt1=r(p)
	qui mat colnames pt1 = treat1
	qui mat p_stand=RW_stand[`k',3]
	qui mat colnames p_stand = treat1
	
	qui ritest treat _b[2.treat]/_se[2.treat] , fixlevels(1) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2  i.st_num_ruta_2   if merge_ind_par==3, vce(cl cct)
	qui matrix pt2=r(p)
	qui mat colnames pt2 = treat2 
	qui mat p_plus=RW_plus[`k',3]
	qui mat colnames p_plus = treat2
	qui est restore `x'
	qui mat foo=pt1,pt2
	qui mat foo2=p_stand,p_plus
	qui estadd matrix p_ri = foo
	qui estadd matrix p_rw = foo2

	qui eststo `x' 

	qui ritest treat  (_b[2.treat]/_se[2.treat]-_b[1.treat]/_se[1.treat]), seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)
	qui matrix p_t3=r(p)
	qui est restore `x'
	qui estadd scalar p_diff2=p_t3[1,1]
	qui estadd scalar p_diff3=RW_diff[`k',3]
	qui eststo `x' 
	local k=`k'+1

} 

* Admin outcomes (cols 5-6):
qui rwolf2 (reg st_enroll_sec_2 treat1 treat2 st_age_tr_2 st_male_tr_2   sc_cal_*  i.sc_strata1_0 , vce(cl cct)) (reg enr_old treat1 treat2 st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)), cluster(cct) indepvars(treat1, treat1) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_stand=e(RW)

qui rwolf2 (reg st_enroll_sec_2 treat1 treat2 st_age_tr_2 st_male_tr_2   sc_cal_*  i.sc_strata1_0 , vce(cl cct)) (reg enr_old treat1 treat2 st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)), cluster(cct) indepvars(treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=e(RW)

qui rwolf2 (reg st_enroll_sec_2 treat0 treat2 st_age_tr_2 st_male_tr_2   sc_cal_*  i.sc_strata1_0 , vce(cl cct)) (reg enr_old treat0 treat2 st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)), cluster(cct) indepvars(treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_diff=e(RW)


local k=1

foreach var of varlist st_enroll_sec_2 enr_old {

	qui reg `var' treat1 treat2 st_age_tr_2 st_male_tr_2 sc_cal_*   i.sc_strata1_0  , vce(cl cct)
	qui estadd scalar obs = e(N)
	qui estadd scalar clusters = e(N_clust)
	qui estadd scalar adjR2 = e(r2_a)	
	qui test treat1=treat2
	qui estadd scalar p_diff=r(p)
	qui eststo `var'

	qui ritest treat _b[1.treat]/_se[1.treat] , fixlevels(2) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `var' i.treat st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)
	qui matrix pt1=r(p)
	qui mat colnames pt1 = treat1
	qui mat p_stand=RW_stand[`k',3]
	qui mat colnames p_stand = treat1

	qui ritest treat _b[2.treat]/_se[2.treat] , fixlevels(1) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `var' i.treat st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)
	qui matrix pt2=r(p)
	qui mat colnames pt2 = treat2 
	qui mat p_plus=RW_plus[`k',3]
	qui mat colnames p_plus = treat2
	qui est restore `var'
	qui mat foo=pt1,pt2
	qui mat foo2=p_stand,p_plus
	qui estadd matrix p_ri = foo
	qui estadd matrix p_rw = foo2
	qui eststo `var'

	qui ritest treat  (_b[2.treat]/_se[2.treat]-_b[1.treat]/_se[1.treat]), seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `var' i.treat st_age_tr_2 st_male_tr_2  sc_cal_*  i.sc_strata1_0, vce(cl cct)
	qui matrix p_t3=r(p)
	qui est restore `var'
	qui estadd scalar p_diff2=p_t3[1,1]
	qui estadd scalar p_diff3=RW_diff[`k',3]
	qui eststo `var' 
	local k=`k'+1

}


estout  st_egra_2_sd st_egma_2_sd st_tot_dum_e_2_sd st_index_2_sd  st_enroll_sec_2 enr_old using "${JPE}/Draft/Tables/table3.tex",  replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]) fmt(%9.3f))  p_ri(par(\{  \}) fmt(%9.3f)) p_rw(par(( )) fmt(%9.3f)) .)  stats(p_diff p_diff2 p_diff3 obs clusters , layout("[@]" "\{@\}" "(@)" "") fmt(%9.3f %9.3f %9.3f %9.0g ) labels("Standard = Plus" " " " " "" "Number of Schools"  "Number of Observations"))  label collabels(none)  mlabels(none) varlabels(treat1 "API Standard"  treat2 "API Plus"  )   keep(treat*) 



**********************************************************************
/* Table 4: Children’s Achievement and Attainment—API Plus Scale-up */
**********************************************************************

use "${JPE}/Data/scaleup.dta", clear

local reps=3000
local seed=123

qui rwolf2 (reg enr_sec_2020 api_1718_new i.treat   confl_sum  i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718)  if expe==1, r)  (reg share_lit_2020 api_1718_new i.treat   confl_sum  i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718)  if expe==1, r) , indepvars(api_1718_new, api_1718_new) usevalid seed(`seed') reps(`reps') nodots 
mat RW_e=e(RW)

qui rwolf2 (reg enr_sec_2020 api_1718_new  i.api_original_2015 confl_sum  i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if exp==0, r) (reg share_lit_2020 api_1718_new  i.api_original_2015 confl_sum i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if exp==0, r) , indepvars(api_1718_new, api_1718_new) usevalid seed(`seed') reps(`reps') nodots 
mat RW_ne=e(RW)


local k=1

foreach x of varlist enr_sec_2020 share_lit_2020{
	qui reg `x' api_1718_new i.treat   confl_sum i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718)  if expe==1 , r		
	qui estadd scalar obs = e(N)
	qui sum `x' if e(sample) & api_1718_new==0
	qui estadd scalar mean = r(mean)
	qui eststo `x'_e 
	
	qui reg `x' api_1718_new i.api_original_2015 confl_sum i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if exp==0, r		
	qui estadd scalar obs = e(N)
	qui sum `x' if e(sample) & api_1718_new==0
	qui estadd scalar mean = r(mean)
	qui eststo `x'_ne

	
	qui ritest api_1718_new _b[api_1718_new]/_se[api_1718_new] , seed(`seed') reps(`reps') nodots: reg `x' api_1718_new i.treat   confl_sum  i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718)  if expe==1 , r
	qui matrix ri=r(p)
	qui mat colnames ri = api_1718_new
	qui mat rw=RW_e[`k',3]
	qui mat colnames rw = api_1718_new
	qui est restore `x'_e
	qui estadd mat p_ri=ri
	qui estadd mat p_rw=rw
	qui eststo `x'_e 
	
	qui ritest api_1718_new _b[api_1718_new]/_se[api_1718_new] , seed(`seed') reps(`reps') nodots: reg `x' api_1718_new i.api_original_2015 confl_sum i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if exp==0, r
	qui matrix ri=r(p)
	qui mat colnames ri = api_1718_new 
	qui mat rw=RW_ne[`k',3]
	qui mat colnames rw = api_1718_new
	qui est restore `x'_ne
	qui estadd mat p_ri=ri
	qui estadd mat p_rw=rw
	qui eststo `x'_ne
	 
	local k=`k'+1
	
} 

estout enr_sec_2020_ne share_lit_2020_ne enr_sec_2020_e share_lit_2020_e using "${JPE}/Draft/Tables/table4.tex",  replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]) fmt(%9.3f))  p_ri(par(\{  \}) fmt(%9.3f)) p_rw(par(( )) fmt(%9.3f)) .)  stats(obs , fmt( %9.0g ) labels("Number of Schools" ))  label collabels(none)  mlabels(none) varlabels(api_1718_new "API Plus ")   keep(api_1718_new) 

**************************************************************************************
/* Figure 2: Differences in the Mentoring Practices Between Experiment and Scale-up */
**************************************************************************************

use "${JPE}/Data/scaleup_mentors.dta", clear
		
local varlist1 "API_staydays  LEC_API_activities API_lectime"
local varlist2 "risk_meetings studrez_API API_studtime "

*Panel A: Quantity of Mentoring
local ind_count = 1		
foreach var in `varlist1'{		

	qui gen treatment_`var' = treatment
	label variable treatment_`var' "`var'"
	local lab: variable label `var'
	label define treatment_`var' 1 "`lab'"
	label value treatment_`var'  treatment_`var'
	egen mean_`var' = mean(`var'), by(cct post)
	bysort cct post: gen obs_cct = _n
	qui reg mean_`var' i.treatment_`var' i.sc_strata i.sc_criterios_3 i.sc_criterios_4 i.treat IM_2010_final  confl_sum if obs_cct==1, cluster(cct)		
	estimates store quant_d_`var'
	drop obs_cct

	local ++ind_count
		
	}
				
coefplot (quant_d_*), keep(1.treatment_*) xline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) levels(95 90) mlcolor(black) mcolor(white) ciopts(lcolor(gs07 black))
graph export "${JPE}/Draft/Figures/fig2_panelA.eps", replace

* Panel B: Quality of Mentoring
drop treatment_*		

local ind_count = 1		
foreach var in `varlist2'{		

	qui gen treatment_`var' = treatment
	label variable treatment_`var' "`var'"
	local lab: variable label `var'
	label define treatment_`var' 1 "`lab'"
	label value treatment_`var'  treatment_`var'
	egen mean_`var' = mean(`var'), by(cct post)
	bysort cct post: gen obs_cct = _n
	qui reg mean_`var' i.treatment_`var' i.sc_strata i.sc_criterios_3 i.sc_criterios_4 i.treat IM_2010_final  confl_sum if obs_cct==1, cluster(cct)		
estimates store qual_d_`var'
drop obs_cct

local ++ind_count
		
	}
			
coefplot (qual_d*), keep(1.treatment_*) xline(0, lcolor(black) lpattern(dash)) graphregion(color(white)) levels(95 90) mlcolor(black) mcolor(white) ciopts(lcolor(gs07 black))
graph export "${JPE}/Draft/Figures/fig2_panelB.eps", replace


*******************************
/* Figure 3: School Closures */
*******************************

use "${JPE}/Data/scaleup.dta", clear

gen school_close_def2018 = 1-open2018
gen school_close_def2020 = 1-open2019


gen api_1718_new_col6 = api_1718_new
gen api_original_2015_col6 = api_original_2015

gen api_1718_new_ne = api_1718_new
gen api_1718_new_e = api_1718_new



qui reg school_close_def2020 api_1718_new_e i.treat confl_sum i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if expe==1 &enr_sec_2020!=.  , r
qui estadd scalar obs = e(N)
qui sum school_close_def2020 if e(sample), de 
qui estadd scalar mean = r(mean)
eststo est_closure_10
qui estadd scalar n_strata = 10 : est_closure_10
estadd local cfa "Yes" : est_closure_10
estadd local sample "2nd Experiment" : est_closure_10


qui reg school_close_def2020 api_1718_new_ne confl_sum  i.api_original_2015 i.(criterio1_1617 criterio2_1617 criterio3_1617 criterio4_1617 criterio5_1617 criterio1_1718 criterio2_1718 criterio3_1718 criterio4_1718 criterio5_1718) if exp==0&up==1  &enr_sec_2020!=., r
qui estadd scalar obs = e(N)
qui sum school_close_def2020 if e(sample), de 
qui estadd scalar mean = r(mean)
eststo est_closure_12
qui estadd scalar n_strata = 10 : est_closure_12
estadd local cfa "Yes" : est_closure_12
estadd local sample "Rest of Chiapas" : est_closure_12


coefplot est_closure_12  est_closure_10    ,vertical  color(gs9)   coeflabels(api_1718_new_ne="Non-Experimental Schools" api_1718_new_e="Experimental Schools") levels(90 95) keep(api_1718_new_ne api_1718_new_e) yline(0, lpattern(dash) lcolor(black)) graphregion(color(white))  msymbol(d) legend(off)  ytitle(Treat Effect on School Closure) xtitle() recast(bar) ciopts(recast(rcap) lcolor(black black black)) citop barwidt(0.3)  offset(0) 
graph export "${JPE}/Draft/Figures/fig3.eps", replace


***********************************
/* Table 5: Parental Investment */
***********************************

* Panel A: First Experiment
use "${JPE}/Data/exp1_parents.dta", clear

global regressors3 "i.enti parent_male"
global covar_par "parent_indig parent_male enl_spanish_2010 enl_math_2010 i.enti"

gen parent_indig=P04_1!=0

gen parent_male=1 if P02==1
replace parent_male=0 if P02==2

gen parent_age=P03
replace parent_age=999 if P03==.

gen missage=(parent_age==999)

foreach var of varlist P17 P21_1 P21_2 P21_3 P21_4 P22_1 P22_2 P22_3 P22_4 P22_5 P22_6 P22_7 {
gen d`var'=1 if `var'==1
replace d`var'=0 if `var'==2
}


gen p_api_meetings= P25*2
gen p_api_visits=P30
recode P27 (8=.) (2=0), gen(p_api_inform)
recode P28 (8=.) (2=0), gen(p_api_adv)

foreach var of varlist p_api_meetings p_api_visits p_api_inform p_api_adv {
	replace `var'=. if treat!=1	
}


rename P16 p_help_hw_2
rename dP17 p_academic_extra_2

rename dP21_1 p_work_for_school_2
rename dP21_2 p_donate_money_2
rename dP21_3 p_manage_resources_2
rename dP21_4 p_donate_in_kind_2
rename dP22_1 p_propose_material_2
rename dP22_2 p_decide_material_2
rename dP22_5 p_evaluate_targets_2
rename dP22_6 p_money_alloc_2
rename dP22_7 p_feeding_LEC_2
rename P18 p_meet_teach_2
rename P19 books
rename P20 read_child

gen p_expec_bac_more_2=(P13>=3 & P13<=6)
replace p_expec_bac_more_2=. if P13>9
swindex p_work_for_school_2 p_donate_money_2 p_donate_in_kind_2 p_feeding_LEC_2, generate(p_engage_school)	
 		 

swindex p_manage_resources_2 p_propose_material_2 p_decide_material_2 p_money_alloc_2  p_evaluate_targets_2, generate(p_engage_manage)

swindex p_help_hw_2 p_academic_extra_2 p_meet_teach_2  p_expec_bac_more_2, generate(p_engage_child)

swindex p_work_for_school_2 p_donate_money_2 p_donate_in_kind_2 p_feeding_LEC_2 p_manage_resources_2 p_propose_material_2 p_decide_material_2 p_money_alloc_2   p_evaluate_targets_2 p_help_hw_2 p_academic_extra_2 p_meet_teach_2  p_expec_bac_more_2, generate(p_engage_all)

foreach x in  p_engage_school  p_engage_manage  p_engage_child p_engage_all p_meet_teach_2 {
	qui sum `x' if treat==0 
	gen `x'_sd = (`x' - r(mean))/r(sd)
}

local reps=3000
local seed=123

qui rwolf2 (reg p_engage_school_sd treat $regressors3, vce(cl ID02)) (reg p_engage_manage_sd treat $regressors3, vce(cl ID02)) (reg p_engage_child_sd treat $regressors3, vce(cl ID02)) (reg p_engage_all_sd treat $regressors3, vce(cl ID02)), cluster(ID02) indepvars(treat, treat, treat, treat) usevalid seed(`seed') reps(`reps') nodots strata(enti)
mat RW_treat=e(RW)


local k=1

foreach var of varlist p_engage_school  p_engage_manage  p_engage_child p_engage_all {
	qui reg `var'_sd treat $regressors3, cluster(ID02)
	qui mat p_rw = RW_treat[`k',3]
	qui mat colnames p_rw = treat
	*qui mat colnames pjoint = treat
	qui estadd matrix p_rw 
	*qui estadd matrix pjoint
	qui sum `var'_sd if treat==0
	qui eret2 scalar mean=r(mean)
	qui eret2 scalar sd=r(sd)
	qui eststo `var'

	qui ritest treat _b[treat]/_se[treat] , seed(`seed') reps(`reps') nodots cluster(ID02)  strata(enti): reg `var' treat $regressors3, cluster(ID02)
	
	qui matrix p_ri=r(p)
	qui mat colnames p_ri = treat
	qui est restore `var'

	qui estadd matrix p_ri

	qui eststo `var' 

	local k=`k'+1
}

estout p_engage_school  p_engage_manage  p_engage_child p_engage_all using "${JPE}/Draft/Tables/table5_panelA.tex", replace style(tex) cells(b(star fmt(%9.3f)) p(par([ ])) p_ri(par(\{  \})) p_rw(par(( ))) .)  stats(N_clust N, fmt(%9.0f) label( "Number of clusters" "Observations")) label collabels(none) mlabels(none) varlabels(treat "API Original")   keep(treat)


* Panel B: Second Experiment
use "${JPE}/Data/exp2.dta", clear
forvalues j = 0(1)2{
gen treat`j' = 0
replace treat`j' = 1 if treat==`j'
replace treat`j' = . if treat==.
}

qui reg st_egra_2 treat1 treat2 i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)			
gen est_sample = e(sample)
	 		 
swindex p_work_for_school_2 p_donate_money_2 p_donate_in_kind_2 p_feeding_LEC_2, generate(p_engage_school)

ren p_manage_school_resources_2 p_manage_resources_2
ren p_propose_school_material_2 p_propose_material_2
ren p_decide_school_material_2 p_decide_material_2
ren p_decide_money_alloc_2 p_money_alloc_2
swindex p_manage_resources_2 p_propose_material_2 p_decide_material_2 p_money_alloc_2   p_evaluate_targets_2, generate(p_engage_manage)

swindex p_help_hw_2 p_academic_extra_2 p_meet_teach_2  p_expec_bac_more_2, generate(p_engage_child)


swindex p_work_for_school_2 p_donate_money_2 p_donate_in_kind_2 p_feeding_LEC_2 p_manage_resources_2 p_propose_material_2 p_decide_material_2 p_money_alloc_2   p_evaluate_targets_2 p_help_hw_2 p_academic_extra_2 p_meet_teach_2  p_expec_bac_more_2, generate(p_engage_all)

foreach x in  p_engage_school  p_engage_manage  p_engage_child p_engage_all p_meet_teach_2 {
	qui sum `x' if treat==0&est_sample==1
	gen `x'_sd = (`x' - r(mean))/r(sd)
}

local reps=3000
local seed=123

qui rwolf2 (reg p_engage_school_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_manage_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_child_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_all_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat1, treat1, treat1, treat1) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_stand=e(RW)

qui rwolf2 (reg p_engage_school_sd treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_manage_sd treat1 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_child_sd treat1 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_all_sd treat1 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=e(RW)

qui rwolf2 (reg p_engage_school_sd treat0 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_manage_sd treat0 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_child_sd treat0 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)) (reg p_engage_all_sd treat0 treat2 p_male_2  p_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_diff=e(RW)

local k=1

foreach x in  p_engage_school_sd  p_engage_manage_sd  p_engage_child_sd p_engage_all_sd {
	qui reg `x' treat1 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)			
	qui estadd scalar obs = e(N)
	qui estadd scalar clusters = e(N_clust)
	qui estadd scalar adjR2 = e(r2_a)
	qui test treat1=treat2
	qui estadd scalar p_diff=r(p)
	qui sum `x' if e(sample) & treat==0
	qui estadd scalar mean = r(mean)
	qui estadd scalar sd=r(sd)
	qui eststo `x' 

	qui ritest treat _b[1.treat]/_se[1.treat] , fixlevels(2) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2  i.st_num_ruta_2   if merge_ind_par==3, vce(cl cct)
	qui matrix pt1=r(p)
	qui mat colnames pt1 = treat1
	qui mat p_stand=RW_stand[`k',3]
	qui mat colnames p_stand = treat1

	qui ritest treat _b[2.treat]/_se[2.treat] , fixlevels(1) seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2  i.st_num_ruta_2   if merge_ind_par==3, vce(cl cct)
	qui matrix pt2=r(p)
	qui mat colnames pt2 = treat2 
	qui mat p_plus=RW_plus[`k',3]
	qui mat colnames p_plus = treat2
	qui est restore `x'
	qui mat foo=pt1,pt2
	qui mat foo2=p_stand,p_plus
	qui estadd matrix p_ri = foo
	qui estadd matrix p_rw = foo2
	qui eststo `x' 

	qui ritest treat  (_b[2.treat]/_se[2.treat]-_b[1.treat]/_se[1.treat]), seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' i.treat st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if merge_ind_par==3, vce(cl cct)
	qui matrix p_t3=r(p)
	qui est restore `x'
	qui estadd scalar p_diff2=p_t3[1,1]
	qui estadd scalar p_diff3=RW_diff[`k',3]
	qui eststo `x' 
	local k=`k'+1

} 

estout  p_engage_school_sd  p_engage_manage_sd  p_engage_child_sd p_engage_all_sd using "${JPE}/Draft/Tables/table5_panelB.tex",  replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]) fmt(%9.3f))  p_ri(par(\{  \}) fmt(%9.3f)) p_rw(par(( )) fmt(%9.3f)) .)  stats(p_diff p_diff2 p_diff3   obs clusters , layout("[@]" "\{@\}" "(@)" "") fmt(%9.3f %9.3f %9.3f  %9.0g ) labels("Standard = Plus" " " " " "" "Observations" "Clusters" ))  label collabels(none)  mlabels(none) varlabels(treat1 "API Standard"  treat2 "API Plus"  )   keep(treat*) 


***********************************************************************************
/* Table 6: The Role of Mentors in Fostering Parental Attitudes—Second Experiment */
***********************************************************************************

* Panel A: Parents and Mentors Interactions (as reported by the parents)
gen p_api_meetings_2_60 = p_api_meetings_2*2
replace p_api_meetings_2_60 = 0 if treat==0
replace p_api_visits_2 = 0 if  treat==0

swindex p_api_meetings_2_60 p_api_visits_2, generate(p_quantity)
swindex p_api_inform p_api_adv, generate(p_quality)


gen mt_convi_2=mt_P26_2_2
gen mt_transition_2=mt_P26_7_2
swindex mt_culture mt_convi mt_feeling mt_communi mt_selfkno mt_learnin mt_transition, generate(mt_index)

local reps=3000
local seed=123

qui rwolf2 (reg p_api_meetings_2_60 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct)) (reg p_api_visits_2 treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct)) (reg p_quantity treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=e(RW)

qui rwolf2 (reg p_api_inform treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct))  (reg p_api_adv treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct)) (reg p_quality treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if treat!=0, vce(cl cct)), cluster(cct) indepvars(treat2, treat2, treat2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=RW_plus\e(RW)


local k=1

foreach x of varlist p_api_meetings_2_60 p_api_visits_2 p_quantity p_api_inform p_api_adv p_quality {
	qui reg `x' treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if est_sample&treat!=0, vce(cl cct)
	qui estadd scalar obs = e(N)
	qui estadd scalar clusters = e(N_clust)
	qui estadd scalar adjR2 = e(r2_a)		
	qui eststo `x' , title(``x_lb'')
	qui ritest treat2 _b[treat2]/_se[treat2] , seed(`seed') reps(`reps') nodots cluster(cct)  strata(sc_strata1_0): reg `x' treat2 st_male_2  st_ind_lang_2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2 if est_sample&treat!=0, vce(cl cct)
	qui matrix pri=r(p)
	qui mat colnames pri = treat2
	qui mat prw=RW_plus[`k',3]
	qui mat colnames prw = treat2
	qui est restore `x'
	qui estadd matrix p_ri = pri
	qui estadd matrix p_rw = prw
	qui eststo `x' 
	local k=`k'+1
}


estout  p_api_meetings_2_60 p_api_visits_2 p_quantity p_api_inform_2 p_api_advises_2 p_quality using "${JPE}/Draft/Tables/table6_panelA.tex",  replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]))  p_ri(par(\{  \})) p_rw(par(( ))) .)  stats(obs ,  fmt(  %9.0g ) labels( "Number of Observations" ))  label collabels(none)  mlabels(none) varlabels(treat2 "API Plus"  )   keep(treat2) 


* Panel B: Parenting Styles that Are Promoted by the Mentors (as reported by the mentors)
collapse mt_culture mt_convi mt_feeling  mt_selfkno mt_transition mt_communi mt_learnin mt_Plus_2 treat2 sc_cal_* sc_strata1_0, by(cct)

swindex mt_communi mt_learnin, generate(mt_index1)
swindex mt_feeling  mt_selfkno mt_transition, generate(mt_index2)

qui rwolf2 (reg mt_feeling mt_Plus_2 sc_cal_* i.sc_strata1_0, r)  (reg mt_selfkno mt_Plus_2 sc_cal_* i.sc_strata1_0 , r) (reg mt_transition mt_Plus_2 sc_cal_* i.sc_strata1_0, r) (reg mt_index2 mt_Plus_2 sc_cal_* i.sc_strata1_0, r) , indepvars(mt_Plus_2, mt_Plus_2, mt_Plus_2 , mt_Plus_2) usevalid seed(`seed') reps(`reps') nodots  strata(sc_strata1_0)
mat RW_plus=e(RW)

qui rwolf2 (reg mt_communi mt_Plus_2 sc_cal_* i.sc_strata1_0, r)  (reg mt_learnin mt_Plus_2 sc_cal_* i.sc_strata1_0, r) (reg mt_index1 mt_Plus_2 sc_cal_* i.sc_strata1_0, r), indepvars(mt_Plus_2, mt_Plus_2, mt_Plus_2) usevalid seed(`seed') reps(`reps') nodots strata(sc_strata1_0)
mat RW_plus=RW_plus\e(RW)

local k=1
foreach x of varlist mt_feeling  mt_selfkno mt_transition mt_index2 mt_communi mt_learnin mt_index1  {	
	qui reg `x' mt_Plus_2 sc_cal_* i.sc_strata1_0, r
	qui estadd scalar obs = e(N)
	qui estadd scalar clusters = e(N_clust)
	qui estadd scalar adjR2 = e(r2_a)		
	qui eststo `x' 
	qui ritest mt_Plus_2 _b[mt_Plus_2]/_se[mt_Plus_2] , seed(`seed') reps(`reps') nodots  strata(sc_strata1_0): reg `x' mt_Plus_2 sc_cal_* i.sc_strata1_0, r
	qui matrix pri=r(p)
	qui mat colnames pri = mt_Plus_2
	qui mat prw=RW_plus[`k',3]
	qui mat colnames prw = mt_Plus_2
	qui est restore `x'
	qui estadd matrix p_ri = pri
	qui estadd matrix p_rw = prw
	eststo `x' 
	local k=`k'+1
}


estout  mt_communication_2 mt_learning_2  mt_index1 mt_feelings_2 mt_selfknowledge_2 mt_transition_2 mt_index2 using "${JPE}/Draft/Tables/table6_panelB.tex",  replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]))  p_ri(par(\{  \})) p_rw(par(( ))) .)  stats(obs ,  fmt( %9.0g ) labels( "Number of Observations" ))  label collabels(none)  mlabels(none) varlabels(treat2 "API Plus"  )   keep(mt_Plus_2) 


*******************************************************
/* Table 7:  School Closures and Parental Engagement */
*******************************************************
use "${JPE}/Data/exp1_schools.dta" , replace

* First Experiment
local regressors "i.enti age missage sex i.margina missmargina"
local regressors2 "i.enti       i.margina missmargina"

replace margina=999 if margina==.
gen missmargina=(margina==999)

rename treat treat1

gen school_closure2014 = 1-exist2014

collapse school_closure2014 enti  treat1 margina missmargina , by(ID02)
qui xi: reg school_closure2014 treat1 `regressors2', r
qui sum school_closure2014 if treat1==0
qui estadd scalar obs = e(N)
qui estadd scalar clusters = e(N_clust)
qui estadd scalar mean=r(mean)
eret2 scalar sd=r(sd)
estimates store close_exp1


* Second Experiment
use "${JPE}/Data/exp2.dta", clear

forvalues j = 0(1)2{
	gen treat`j' = 0
	replace treat`j' = 1 if treat==`j'
	replace treat`j' = . if treat==.
	}	

gen school_close_definit = 1-merge_f9112018

local ind = 1 
foreach x in p_manage_school_resources_2 p_propose_school_material_2  p_evaluate_targets_2 p_decide_school_material_2 p_decide_money_alloc_2 {
	gen var_manag_`ind' = `x'
	local ++ind
	}

swindex p_work_for_school_2 p_donate_money_2 p_donate_in_kind_2 p_feeding_LEC_2 var_manag_1 var_manag_2 var_manag_3 var_manag_4 var_manag_5 p_help_hw_2 p_academic_extra_2 p_meet_teach_2  p_expec_bac_more_2 , generate(total_p_engage)

qui sum total_p_engage if treat==0
gen total_p_engage_sd = (total_p_engage - r(mean))/r(sd)
	
preserve
collapse school_close_definit treat1 treat2   sc_cal_*  sc_strata1_0  st_week_int_2  st_num_ruta_2   if merge_ind_par==3 , by(cct)

xi: qui reg school_close_definit treat1 treat2 sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  ,r 
qui estadd scalar obs = e(N)
qui estadd scalar clusters = e(N_clust)
qui test treat1=treat2
eret2 scalar pval_tt=r(p)
eststo close_exp2  
restore

qui ivreg2 school_close_definit ( total_p_engage_sd  = treat2)  treat1 st_male_2  st_ind_lang_2 c.sc_cal_* i.sc_strata1_0 i.st_week_int_2 i.st_num_ruta_2  if merge_ind_par==3    , cluster(cct)
qui estadd scalar obs = e(N)
qui estadd scalar clusters = e(N_clust)
qui estadd scalar F_first=e(widstat)
eststo close_iv 

estout close_exp1 close_exp2 close_iv using "${JPE}/Draft/Tables/table7.tex", replace style(tex) cells(b(nostar fmt(%9.3f)) p(par([ ]))) stats(   obs clusters F_first , fmt( %9.0g %9.0g  %9.3f ) labels(  "Observations" "Clusters" "F-Stat (Excl. Instruments)"))    starlevels(* 0.10 ** 0.05 *** 0.01)  label collabels(none)     mlabels(none) varlabels( treat1 "API Original"  treat2 "API Plus"   total_p_engage_sd "Overall Parental Engagement" )   keep( treat1 treat2   total_p_engage_sd) order( treat1 treat2   total_p_engage_sd) 



